/*
 * Copyright (C) 2014 Nathan T
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package emergence.game.combat.ability;

import emergence.entities.Mob;
import emergence.game.Game;
import emergence.game.gfx.ColoredSprite;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.ImageIcon;

/**
 *
 * @author Nathan T
 */
public abstract class Ability {

    public static final int SCALE = 1;
    
    protected final String name;
    protected final String description;
    protected final ColoredSprite sprite;
    protected final ImageIcon icon;
    protected ABILITY_ANIM animType;
    
    protected boolean requiresMobTarget;
    protected boolean requiresPointTarget;

    protected boolean endsCombatTurn;
    protected int selfHeal = 0;
    protected int damage = 0;

    public Ability(String name, String description, ColoredSprite sprite) {
        this.name = name;
        this.description = description;
        this.sprite = sprite;
        this.icon = getImageIcon();
    }

    protected final ImageIcon getImageIcon() {
        BufferedImage bufferedIconImage = new BufferedImage(sprite.width(), sprite.height(), BufferedImage.TYPE_INT_RGB);
        int imagePixels[] = ((DataBufferInt) bufferedIconImage.getRaster().getDataBuffer()).getData();

        for (int i = 0; i < sprite.pixels().length; i++) {
            for (int j = 0; j < sprite.pixels()[0].length; j++) {
                if (sprite.pixels()[i][j] < 255) {
                    imagePixels[i + (j * sprite.width())] = Game.colors[sprite.pixels()[i][j]];
                }
            }
        }

        Image resizedImage = bufferedIconImage.getScaledInstance(sprite.width() * SCALE, sprite.height() * SCALE, Image.SCALE_SMOOTH);
        return new ImageIcon(resizedImage);
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
    
    public ColoredSprite getSprite() {
        return sprite;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public boolean requiresMobTarget() {
        return this.requiresMobTarget;
    }
    
    public boolean requiresPointTarget() {
        return this.requiresPointTarget;
    }
    
    public void activateAbility(Mob caster, Mob target, Point targetPoint) {
        payCosts();
        
        if (caster != null) {
            caster.gainHealth(selfHeal);
        }
        if (target != null) {
            target.takeDamage(damage);
        }
        if ((caster != null) && endsCombatTurn) {
            caster.endCombatTurn();
        }
    }
    
    protected void payCosts() {
        // Fill in code
    }
    
    public boolean endsCombatTurn() {
        return this.endsCombatTurn;
    }

    public int getSelfHeal() {
        return this.selfHeal;
    }

    public int getDamage() {
        return this.damage;
    }

    public enum ABILITY_ANIM {

        CAST,
        ATTACK
    }

}
